﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace ArgusPCLib.Mathematics
{
	public class ExpressionFormatInfo : IFormatProvider
	{
		public string[] OpeningBrackets { get; set; }
		public string[] ClosingBrackets { get; set; }
		public IFormatProvider[] OperandFormatProviders { get; set; }
		public CultureInfo CultureInfo { get; set; }

		public ExpressionFormatInfo()
		{
			this.OpeningBrackets = new string[] { "(" };
			this.ClosingBrackets = new string[] { ")" };
			this.OperandFormatProviders = null;
			this.CultureInfo = CultureInfo.InvariantCulture;
		}

		public object GetFormat(Type formatType)
		{
			if (formatType == typeof(ExpressionFormatInfo))
				return this;
			if (formatType == typeof(CultureInfo))
				return this.CultureInfo;

			if (this.OperandFormatProviders != null)
			{
				foreach (IFormatProvider formatProvider in this.OperandFormatProviders)
				{
					object RetVal = formatProvider.GetFormat(formatType);
					if (RetVal != null)
						return RetVal;
				}
			}
			return null;
		}

		public static ExpressionFormatInfo InvariantInfo { get { return new ExpressionFormatInfo(); } }

		public static ExpressionFormatInfo GetInstance(IFormatProvider formatProvider)
		{
			if (formatProvider == null)
				return ExpressionFormatInfo.InvariantInfo;
			ExpressionFormatInfo efi = formatProvider.GetFormat(typeof(ExpressionFormatInfo)) as ExpressionFormatInfo;
			if (efi == null)
				return ExpressionFormatInfo.InvariantInfo;
			return efi;
		}
	}
}
